<!DOCTYPE html>
<html>
<head>
    <title>角色列表</title>
    <link rel="stylesheet" href="${request.contextPath}/resources/layui/css/layui.css">
    <link rel="stylesheet" href="${request.contextPath}/resources/dtree/dtree.css">
    <link rel="stylesheet" href="${request.contextPath}/resources/dtree/font/dtreefont.css">
</head>
<body>
<div style="margin-left: 10px;margin-top: 10px">
    <form class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">角色名称</label>
                <div class="layui-input-inline" style="width: 150px">
                    <input type="text" class="layui-input" id="name" placeholder="角色名称" autocomplete="off">
                </div>
            </div>
            <div class="layui-inline">
                <button type="button" class="layui-btn" id="searchBtn">查询</button>
            </div>
        </div>
    </form>
    <hr>
    <table id="dataTable" lay-filter="dataTableFilter"></table>
</div>

<script type="text/html" id="editTpl">
    <form class="layui-form layui-form-pane" lay-filter="editFormFilter" style="margin-top: 15px;margin-left: 15px">
        <div class="layui-form-item">
            <label class="layui-form-label">
                角色名称
            </label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="name" placeholder="角色名称" lay-verify="required" lay-reqText="角色名称不能为空" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色标识</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="tag" placeholder="角色标识" lay-verify="required" lay-reqText="角色标识不能为空" />
            </div>
        </div>
        <div class="layui-form-item layui-form-text" style="width: 300px">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" name="descp"></textarea>
            </div>
        </div>
        <button id="subBtn" type="button" lay-filter="subBtnFilter" lay-submit style="display: none"></button>
    </form>
</script>
<script type="text/html" id="setRolePerTpl">
    <ul id="permissions" class="dtree" data-id="0"></ul>
</script>

<script type="text/html" id="headerBtns">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" type="button" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>添加
        </button>
    </div>
</script>
<script type="text/html" id="rowBtns">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm layui-btn-warm" type="button" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>修改
        </button>
        <button class="layui-btn layui-btn-sm" type="button" lay-event="setting">
            <i class="layui-icon layui-icon-set"></i>设置权限
        </button>
    </div>
</script>


<script src="${request.contextPath}/resources/layui/layui.js"></script>
<script>
    var cxt = "${request.contextPath}";
    layui.extend({
        dtree: cxt+'/resources/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
    }).use(['dtree','form', 'table', 'jquery', 'layer'], function () {
        let cxt = "${request.contextPath}";
        let form = layui.form;
        let table = layui.table;
        let $ = layui.jquery;
        let layer = layui.layer;
        let dtree = layui.dtree;

        let tabOpt = {
            elem: "#dataTable",
            id: "dataTableId",
            url: cxt + "/sysrole/page.do",
            toolbar:"#headerBtns",
            cols: [[
                {field: "name", title: "角色名"},
                {field: "tag", title: "标识"},
                {field: "descp", title: "描述",minWidth:180},
                {title: "操作",toolbar:"#rowBtns",minWidth:190,fixed:"right"}
            ]],
            page: true,
            parseData: function (rs) {
                if (rs.code == 200) {
                    return {
                        code: rs.code,
                        msg: rs.msg,
                        count: rs.data.total,
                        data: rs.data.records
                    }
                }
            },
            response: {
                statusCode: 200
            }
        };
        //渲染数据表格
        let tabIns = table.render(tabOpt);
        //搜索
        $("#searchBtn").click(function (){
            let name = $("#name").val();
            tabIns.reload({
                where:{
                    name:name,
                }
            })
        });
        //头工具栏事件
        table.on("toolbar(dataTableFilter)",function (d){
            let event = d.event;
            if(event == "add"){
                //调用具体的add
                add();
            }
        })

        /**
         * 调用具体的add
         */
        function add(){
            let layerOpt = {
                title:"编辑",
                type:1,
                content:$("#editTpl").html(),
                area:['330px','460px'],
                btn:['确认','取消'],
                btnAlign:'c',
                anim:4,
                success:function (layero,index){
                    //渲染表单
                    form.render();
                    //设置表单提交监听事件
                    form.on("submit(subBtnFilter)",function (d){
                        let formData = d.field;
                        //进行ajax提交
                        $.post(cxt+"/sysrole/add.do",formData,function (rs){
                            layer.msg(rs.msg);
                            if(rs.code != 200){
                                return false;
                            }
                            //刷新表格
                            $("#searchBtn").click();
                            //关闭弹层
                            layer.close(index);
                        });

                        return false;//阻止默认提交行为
                    });
                },
                yes:function (index,layero){
                    $("#subBtn").click();
                }
            };
            layer.open(layerOpt);
        }
        //行工具栏事件
        table.on("tool(dataTableFilter)",function (d){
            let event = d.event;
            let rowData = d.data;
            if(event == "edit"){
                //调用具体的重置请求
                edit(rowData);
            }else if(event == "setting"){
                setting(rowData);
            }
        })

        /**
         * 修改角色
         */
        function edit(rowData){
            let layerOpt = {
                title:"编辑",
                type:1,
                content:$("#editTpl").html(),
                area:['330px','460px'],
                btn:['确认','取消'],
                btnAlign:'c',
                anim:4,
                success:function (layero,index){
                    //表单赋值
                    form.val("editFormFilter",rowData);
                    //设置表单提交监听事件
                    form.on("submit(subBtnFilter)",function (d){
                        let formData = d.field;
                        formData.id = rowData.id;//为角色ID 赋值
                        //进行ajax提交
                        $.post(cxt+"/sysrole/update.do",formData,function (rs){
                            layer.msg(rs.msg);
                            if(rs.code != 200){
                                return false;
                            }
                            //刷新表格
                            $("#searchBtn").click();
                            //关闭弹层
                            layer.close(index);
                        });

                        return false;//阻止默认提交行为
                    });
                },
                yes:function (index,layero){
                    $("#subBtn").click();
                }
            };
            layer.open(layerOpt);
        }

        /**
         * 设置角色权限
         * @param rowData
         */
        function setting(rowData){
            let layerOpt = {
                title:"设置角色权限",
                type:1,
                content:$("#setRolePerTpl").html(),
                area:['330px','660px'],
                btn:['确认','取消'],
                btnAlign:'c',
                offset:"t",
                anim:4,
                success:function (layero,index){
                    //渲染 dtree
                    //渲染当前页面左侧的菜单
                    let treeOpt = {
                        elem:"#permissions",//树容器dom
                        url:cxt+'/sysrole/rolePers.do?roleId='+rowData.id,//需要渲染成树的数据接口
                        dataStyle: "layuiStyle",//数据风格使用layui风格
                        response:{message:"msg",statusCode:200},
                        dataFormat: "list",
                        ficon:["1","5"],//第一个图标  有子节点和没有子节点
                        icon:["-1","-1"],//第二个图标
                        checkbar:true
                    };
                    let permissions = dtree.render(treeOpt);
                },
                yes:function (index,layero){
                    var params = dtree.getCheckbarNodesParam("permissions");
                    console.log(params);
                    let pIds = new Array();
                    for(p of params){
                        pIds.push("pId="+p.nodeId);//将所有勾选的节点的ID 放入到数组中
                    }
                    let  roleId = rowData.id;
                    pIds = pIds.join("&");
                    $.post(cxt+"/sysrole/setPermissions.do?roleId="+roleId+"&"+pIds,function (rs){
                        layer.msg(rs.msg);
                        if(rs.code != 200){
                            return false;
                        }
                        layer.close(index);
                    })
                }
            };
            layer.open(layerOpt);
        }




    })
</script>
</body>
</html>
